 // @ 516
// _(:з」∠)_

define([
    'echarts',
    'antv.G2'
], function( echarts ) {
    'use strict';

    const Chartx = { chartArry: {} };

    // 温度 - echarts
    Chartx["temp_echarts"] = function( param ) {
        let option = {
            color:['#ff0000','#ffc500','#00d3ff'],
            xAxis: {
                type: 'category',
                data: []
            },
            yAxis: {
                name: '气温(℃)',
                type: 'value',
                axisPointer: { snap: true }
            },
            tooltip: {
                trigger: 'axis',
                axisPointer : { type : 'shadow' }
                // formatter: function ( params ) {
                //     var res = '<div><p>时间：'+params[0].name+'</p></div>' 
                //     for ( let x = 0; x < params.length; x++) {
                //         if ( params[x].data != "-" ) {
                //             res += '<p>'+params[x].seriesName+': '+params[x].data+' ℃</p>'
                //         }
                //     }
                //     return res;
                // },
            },
            toolbox: {
                show: true,
                feature: {
                    saveAsImage: {}
                }
            },
            legend: {
                data:['历史值', '实况值', '常年值'],
                x: 'center', y: 'bottom'
            },
            grid: {
                top: 35,
                left: 30,
                right: 20,
                bottom: 50,
                containLabel: false
            },
            series: [{
                name: '历史值',
                type: 'line',
                smooth: true,
                data: []
            }, {
                name: '实况值',
                type: 'line',
                smooth: true,
                data: []
            }, {
                name: '常年值',
                type: 'line',
                smooth: true,
                data: []
            }]
        };

        if ( !param.elmt ) return false;
        let elmtId = param.elmt.id;
        if ( Chartx.chartArry[ elmtId ] ) {
            let thisChart = Chartx.chartArry[ elmtId ];
            thisChart.dispose();
        }

        Chartx.chartArry[ elmtId ] = echarts.init( param.elmt );
        Chartx.chartArry[ elmtId ].setOption( option );
    }

    // 降水 - echarts
    Chartx["prec_echarts"] = function( param ) {
        let option = {
            color: [ '#ffc500', '#3398DB', '#00d3ff' ],
            tooltip : {
                trigger: 'axis',
                axisPointer : { type : 'shadow' }
            },
            grid: {
                top: 35,
                left: 40,
                right: 20,
                bottom: 50,
                containLabel: false
            },
            xAxis : [
                {
                    type : 'category',
                    data : [],
                    axisTick: { alignWithLabel: true }
                }
            ],
            yAxis: [
                {
                    name: '降水量(mm)',
                    type: 'value',
                    min: 0,
                    axisPointer: { snap: true }
                }
            ],
            legend: {
                data:[ '历史降水', '实况降水', '常年降水'],
                x: 'center', y: 'bottom'
            },
            series: [{
                name: '历史降水',
                type: 'bar',
                barWidth: '20%',
                data: []
            }, {
                name: '实况降水',
                type: 'bar',
                barWidth: '20%',
                data: []
            }, {
                name: '常年降水',
                type: 'bar',
                barWidth: '20%',
                data: []
            }]
        };

        if ( !param.elmt ) return false;
        let elmtId = param.elmt.id;
        if ( Chartx.chartArry[ elmtId ] ) {
            let thisChart = Chartx.chartArry[ elmtId ];
            thisChart.dispose();
        }

        Chartx.chartArry[ elmtId ] = echarts.init( param.elmt );
        Chartx.chartArry[ elmtId ].setOption( option );
    }

    // 灾害详情
    Chartx["dis_echarts"] = function( param ) {
        let option = {
            color: [ '#ffc500', '#3398DB', '#00d3ff' ],
            tooltip : {
                trigger: 'axis',
                axisPointer : { type : 'shadow' }
            },
            grid: {
                top: 30,
                left: 60,
                right: 10,
                bottom: 50,
                containLabel: false
            },
            xAxis : [
                {
                    type : 'category',
                    data : [],
                    axisTick: { alignWithLabel: true }
                }
            ],
            yAxis: [
                {
                    name: param.yAxis,
                    type: 'value', min: 0,
                    axisPointer: { snap: true }
                }
            ],
            legend: {
                data:[ '轻度', '中度', '重度'],
                x: 'center', y: 'bottom'
            },
            series: [{
                name: '轻度',
                type: 'bar',
                barWidth: '20%',
                data: []
            }, {
                name: '中度',
                type: 'bar',
                barWidth: '20%',
                data: []
            }, {
                name: '重度',
                type: 'bar',
                barWidth: '20%',
                data: []
            }]
        };

        if ( !param.elmt ) return false;
        let elmtId = param.elmt.id;
        if ( Chartx.chartArry[ elmtId ] ) {
            let thisChart = Chartx.chartArry[ elmtId ];
            thisChart.dispose();
        }

        Chartx.chartArry[ elmtId ] = echarts.init( param.elmt );
        Chartx.chartArry[ elmtId ].setOption( option );
    }

    // ndvi(植被)
    Chartx["ndvi"] = function( param ) {

        if ( !param.elmt ) return false;
        let elmtId = param.elmt.id;
        if ( Chartx.chartArry[ elmtId ] ) Chartx.chartArry[ elmtId ].destroy();

        Chartx.chartArry[ elmtId ] = new G2.Chart({
            id: elmtId,
            width: param.width,
            height: param.height,
            padding: param.padding
        });

        Chartx.chartArry[ elmtId ].source( param.data, {
            time: { alias: "时间" },
            value: { alias: "NDVI", max: 0.4, min: 0.25 }
        });

        Chartx.chartArry[ elmtId ].axis('value', {
            formatter: function( val ) {
                return val + "";
            },
            title: {
                fontSize: '12',
                textAlign: 'center',
                fill: '#333'
            },
            line: {
                lineWidth: 1,
                stroke: '#999',
            }
        });

        Chartx.chartArry[ elmtId ]._attrs.viewTheme.colors = [ '#000000', '#4ecb73' ];
        Chartx.chartArry[ elmtId ].line().shape('smooth').position('time*value').color('legend');

        Chartx.chartArry[ elmtId ].legend({ 
            position: 'bottom',
            itemGap: 20
        });

        if ( param.guideLine ) {
            for ( let x = 0; x < param.guideLine.length; x++ ) {
                let guideData = param.guideLine[x];
                Chartx.chartArry[ elmtId ].guide().line({
                    top: true,
                    start: ['min', guideData.min],
                    end: ['max', guideData.max],
                    lineStyle: { stroke: guideData.color, lineWidth: 1 },
                    text: {
                        content: guideData.content,
                        position: 'start', offsetX: guideData.offsetX, offsetY: guideData.offsetY,
                        style: { fontSize: 12, fill: guideData.color, opacity: 0.5 }
                    }
                });
            }
        }

        Chartx.chartArry[ elmtId ].render();
    }

    window.Chartx = Chartx;

    return Chartx;
});